#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //调用程序初始化函数
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

//程序初始化函数,程序启动时最先调用的自定义函数
void MainWindow::init()
{
    //初始化界面
    this->setCentralWidget(ui->stackedWidget);//设置stackwidget为中心组件
    ui->toolBarAdmin->hide();//隐藏管理员工具栏
    ui->toolBarUser->hide();//隐藏用户工具栏

    //初始化数据库连接
    m_db=QSqlDatabase::addDatabase("QMYSQL");//添加mysql驱动
    m_db.setUserName(g_dbUserName);//设置用户名
    m_db.setPassword(g_dbPswd);//设置用户密码
    m_db.setDatabaseName("db_managerSys");//设置数据库名
    if(!m_db.open()){//打开数据库
        QMessageBox::warning(this,"警告","数据库连接失败.");
        return;
    }
}

void MainWindow::adminInit()
{
    //显示管理员工具栏
    ui->toolBarAdmin->show();//显示管理员工具栏
    ui->toolBarUser->hide();//隐藏用户工具栏

    //申请管理
    QSqlTableModel* model=new QSqlTableModel(this,m_db);//初始化申请管理表格对象
    model->setTable("tb_apply");//设置数据表名
    model->select();//加载表格数据
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);//设置表格提交模式为手动提交
    model->setHeaderData(0,Qt::Horizontal,"编号");//设置表头
    model->setHeaderData(1,Qt::Horizontal,"申请人");
    model->setHeaderData(2,Qt::Horizontal,"申请装备");
    model->setHeaderData(3,Qt::Horizontal,"状态");
    ui->tvApplyAdmin->setSelectionBehavior(QAbstractItemView::SelectRows);//设置视图以行为单位选中
    ui->tvApplyAdmin->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//设置视图自动拉伸
    ui->tvApplyAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);//默认不可编辑表格
    ui->tvApplyAdmin->setModel(model);//绑定视图模型
    ui->stackedWidget->setCurrentWidget(ui->pagAdmin);//切换至申请管理界面
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagApplyAdmin);//设置当前界面为申请管理界面

    //仓库管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_git");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"名称");
    model->setHeaderData(1,Qt::Horizontal,"数量");
    ui->tvGitAdmin->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvGitAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvGitAdmin->setModel(model);

    //采购管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_buy");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"名称");
    model->setHeaderData(1,Qt::Horizontal,"数量");
    model->setHeaderData(2,Qt::Horizontal,"供应商");
    ui->tvBuyAdmin->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvBuyAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvBuyAdmin->setModel(model);

    //装备使用管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_used");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"用户账号");
    model->setHeaderData(2,Qt::Horizontal,"用户名");
    model->setHeaderData(3,Qt::Horizontal,"装备名");
    ui->tvUsedAdmin->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvUsedAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvUsedAdmin->setModel(model);

    //装备维护管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_fix");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"申请人");
    model->setHeaderData(2,Qt::Horizontal,"维修装备");
    model->setHeaderData(3,Qt::Horizontal,"损坏程度");
    model->setHeaderData(4,Qt::Horizontal,"状态");
    ui->tvFixAdmin->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvFixAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvFixAdmin->setModel(model);

    //装备b报废管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_abolish");
    model->select();
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"申请人");
    model->setHeaderData(2,Qt::Horizontal,"报废装备");
    model->setHeaderData(3,Qt::Horizontal,"原因");
    model->setHeaderData(4,Qt::Horizontal,"状态");
    ui->tvAbolishAdmin->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvAbolishAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvAbolishAdmin->setModel(model);

}

void MainWindow::userInit()
{
    //显示管理员工具栏
    ui->toolBarUser->show();//显示用户工具栏
    ui->toolBarAdmin->hide();//隐藏管理员工具栏

    //申请管理
    QSqlTableModel* model=new QSqlTableModel(this,m_db);
    model->setTable("tb_apply");
    model->select();

    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"申请人");
    model->setHeaderData(2,Qt::Horizontal,"申请装备");
    model->setHeaderData(3,Qt::Horizontal,"状态");
    ui->tvResultUser->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tvResultUser->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvResultUser->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvResultUser->setModel(model);
    ui->stackedWidget->setCurrentWidget(ui->pagUser);
    ui->stackedWidgetUser->setCurrentWidget(ui->pagResultUser);

    //遍历所有申请管理数据记录,只显示本人的申请记录
    for(int i=0;i<model->rowCount();++i){
        ui->tvResultUser->hideRow(i);//隐藏记录
        if(model->data(model->index(i,1)).toString()==m_userName){//判断是否是本人的用户名
            ui->tvResultUser->showRow(i);//显示记录
        }
    }

    //维修管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_fix");
    model->select();

    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"申请人");
    model->setHeaderData(2,Qt::Horizontal,"维修装备");
    model->setHeaderData(3,Qt::Horizontal,"损坏程度");
    model->setHeaderData(4,Qt::Horizontal,"状态");
    ui->tvFixUser->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tvFixUser->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvFixUser->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvFixUser->setModel(model);


    //遍历所有申请管理数据记录,只显示本人的申请记录
    for(int i=0;i<model->rowCount();++i){
        ui->tvFixUser->hideRow(i);//隐藏记录
        if(model->data(model->index(i,1)).toString()==m_userName){//判断是否是本人的用户名
            ui->tvFixUser->showRow(i);//显示记录
        }
    }

    //报废管理
    model=new QSqlTableModel(this,m_db);
    model->setTable("tb_abolish");
    model->select();

    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setHeaderData(0,Qt::Horizontal,"编号");
    model->setHeaderData(1,Qt::Horizontal,"申请人");
    model->setHeaderData(2,Qt::Horizontal,"报废装备");
    model->setHeaderData(3,Qt::Horizontal,"原因");
    model->setHeaderData(4,Qt::Horizontal,"状态");
    ui->tvAbolistUser->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tvAbolistUser->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvAbolistUser->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvAbolistUser->setModel(model);

    //遍历所有申请管理数据记录,只显示本人的申请记录
    for(int i=0;i<model->rowCount();++i){
        ui->tvAbolistUser->hideRow(i);//隐藏记录
        if(model->data(model->index(i,1)).toString()==m_userName){//判断是否是本人的用户名
            ui->tvAbolistUser->showRow(i);//显示记录
        }
    }

}

void MainWindow::on_btnQuitLogin_clicked()
{
    int result=QMessageBox::question(this,"询问","确定要退出程序吗?");//弹出退出提示框
    if(result==QMessageBox::Yes){//如果用户选择yes则,退出程序
        this->close();
    }
}


void MainWindow::on_btnLoginLogin_clicked()
{
    //检测输入是否完整
    QString account=ui->editAccountLogin->text();//获取界面用户名输入
    QString pswd=ui->editPswdLogin->text();//获取界面密码输入
    if(account.isEmpty()||pswd.isEmpty()){//如果密码或账号为空则提示输入不完整
        QMessageBox::warning(this,"警告","请输入完整的信息.");
        return;
    }

    //z作为管理员登录
    if(ui->radioAdmin->isChecked()){//如果管理员raido被选中
        QSqlQuery query(m_db);//初始化sql语句对象
        query.exec(QString("select * from tb_admin where account='%1';").arg(account));//执行sql语句查询数据
        query.first();//切换至第一行查询到的数据
        if(query.isValid()){//如果记录可用
            if(query.value("pswd").toString()==pswd){//判断密码是否正确
                m_userAccount=account;//记录登录用户账号
                m_userName=query.value("name").toString();//记录登录用户姓名
                m_uerRole=1;//记录登录用户角色
                adminInit();//管理员界面初始化
            }else{//密码错误
                QMessageBox::warning(this,"警告","密码错误.");
                return;
            }
        }
        else{//账号错误
            QMessageBox::warning(this,"警告","用户名错误.");
            return;
        }
    }else if(ui->radioUser->isChecked()){//作为用户登录
        QSqlQuery query(m_db);
        query.exec(QString("select * from tb_user where account='%1';").arg(account));
        query.first();
        if(query.isValid()){
            if(query.value("pswd").toString()==pswd){
                m_userAccount=account;
                m_userName=query.value("name").toString();
                m_uerRole=0;
                userInit();//用户界面初始化
            }else{//密码错误
                QMessageBox::warning(this,"警告","密码错误.");
                return;
            }
        }
        else{//账号错误
            QMessageBox::warning(this,"警告","用户名错误.");
            return;
        }
    }
}


void MainWindow::on_btnPassApplyAdmin_clicked()
{
    QModelIndex index=ui->tvApplyAdmin->currentIndex();//获取当前申请管理界面的选中行
    if(index.isValid()){//如果选中行可用
        QAbstractItemModel* model=ui->tvApplyAdmin->model();//获取申请界面绑定的模型
        model->setData(model->index(index.row(),3),"通过");//修改模型数据
        ui->tvApplyAdmin->setCurrentIndex(QModelIndex());//置空选中行
    }
}


void MainWindow::on_btnRefuseApplyAdmin_clicked()
{
    QModelIndex index=ui->tvApplyAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvApplyAdmin->model();
        model->setData(model->index(index.row(),3),"不通过");//修改模型数据
        ui->tvApplyAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnDeleteApplyAdmin_clicked()
{
    QModelIndex index=ui->tvApplyAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvApplyAdmin->model();
        model->removeRow(index.row());//移除数据记录
        ui->tvApplyAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetApplyAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvApplyAdmin->model());//获取模型绑定的数据模型
    model->select();//重新从数据库加载数据到模型
    ui->tvApplyAdmin->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubminApplyAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvApplyAdmin->model());
    if(model->isDirty()){//如果模型数据被修改过
        model->submitAll();//提交所有修改到数据库
        QMessageBox::information(this,"提示","提交成功!");//弹出修改成功提示框
    }
}


void MainWindow::on_actApplyAdmin_triggered()
{
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagApplyAdmin);//切换到申请管理界面
}


void MainWindow::on_actGitAdmin_triggered()
{
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagGitAdmin);//切换到装备库存管理界面
}


void MainWindow::on_cheEditGitAdmin_stateChanged(int arg1)
{
    if(arg1){//如果选中编辑框,则启用对表格的编辑
        ui->tvGitAdmin->setEditTriggers(QAbstractItemView::AllEditTriggers);
    }else{//如果不选中编辑框,则禁用对模型的编辑
        ui->tvGitAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}


void MainWindow::on_btnResetGitAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvGitAdmin->model());
    model->select();
    ui->tvGitAdmin->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitGitAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvGitAdmin->model());
    if(model->isDirty()){
        model->submitAll();
        QMessageBox::information(this,"提示","提交成功!");
    }
}


void MainWindow::on_btnAppendGitAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvGitAdmin->model());
    model->insertRow(model->rowCount());
}


void MainWindow::on_btnDeleteGitAdmin_clicked()
{
    QModelIndex index=ui->tvGitAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvGitAdmin->model();
        model->removeRow(index.row());
        ui->tvGitAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_actBuyAdmin_triggered()
{
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagBuyAdmin);
}


void MainWindow::on_cheEditBuyAdmin_stateChanged(int arg1)
{
    if(arg1){
        ui->tvBuyAdmin->setEditTriggers(QAbstractItemView::AllEditTriggers);
    }else{
        ui->tvBuyAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}


void MainWindow::on_btnAppendBuyAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvBuyAdmin->model());
    model->insertRow(model->rowCount());
}


void MainWindow::on_btnDeleteBuyAdmin_clicked()
{
    QModelIndex index=ui->tvBuyAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvBuyAdmin->model();
        model->removeRow(index.row());
        ui->tvBuyAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetBuyAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvBuyAdmin->model());
    model->select();
    ui->tvBuyAdmin->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitBuyAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvBuyAdmin->model());
    if(model->isDirty()){
        model->submitAll();
        QMessageBox::information(this,"提示","提交成功!");
    }
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    int result=QMessageBox::question(this,"询问","确定要退出程序吗?");//弹出确认退出程序对话框
    if(result==QMessageBox::Yes){//如果选择yes
        m_db.close();//关闭数据库连接
        e->accept();//关闭程序
    }else{//如果点击其他按钮
        e->ignore();//忽略关闭事件
        return;
    }
}


void MainWindow::on_cheEditUsedAdmin_stateChanged(int arg1)
{
    if(arg1){
        ui->tvUsedAdmin->setEditTriggers(QAbstractItemView::AllEditTriggers);
    }else{
        ui->tvUsedAdmin->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}


void MainWindow::on_btnAppendUsedAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvUsedAdmin->model());
    model->insertRow(model->rowCount());//添加表格行
}


void MainWindow::on_btnDeleteUsedAdmin_clicked()
{
    QModelIndex index=ui->tvUsedAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvUsedAdmin->model();
        model->removeRow(index.row());
        ui->tvUsedAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetUsedAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvUsedAdmin->model());
    model->select();
    ui->tvUsedAdmin->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitUsedAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvUsedAdmin->model());
    if(model->isDirty()){
        model->submitAll();
        QMessageBox::information(this,"提示","提交成功!");
    }
}


void MainWindow::on_actUsedAmin_triggered()
{
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagUsedAdmin);
}


void MainWindow::on_actQuit_triggered()
{
    this->close();//关闭程序
}


void MainWindow::on_btnFindGitAdmin_clicked()
{
    QString text=ui->editFindGitAdmin->text();//获取查找输入框的查找内容
    QAbstractItemModel* model=ui->tvGitAdmin->model();//获取视图绑定的模型
    if(text.isEmpty()){//如果查找内容为空,则显示说有数据行
        for(int i=0;i<model->rowCount();++i){
            ui->tvGitAdmin->showRow(i);
        }
    }else{//如果查找内容不为空
        for(int i=0;i<model->rowCount();++i){
            ui->tvGitAdmin->hideRow(i);//隐藏改行
            if(model->data(model->index(i,0)).toString().contains(text)){//如果改行含有查找内容
                 ui->tvGitAdmin->showRow(i);//显示改行
            }
        }
    }
}


void MainWindow::on_btnFindUsedAdmin_clicked()
{
    QString text=ui->editFindUsedAdmin->text();
    QAbstractItemModel* model=ui->tvUsedAdmin->model();
    if(text.isEmpty()){
        for(int i=0;i<model->rowCount();++i){
            ui->tvUsedAdmin->showRow(i);
        }
    }else{
        for(int i=0;i<model->rowCount();++i){
            ui->tvUsedAdmin->hideRow(i);
            if(model->data(model->index(i,0)).toString().contains(text)||model->data(model->index(i,3)).toString().contains(text)){
                ui->tvUsedAdmin->showRow(i);
            }
        }
    }
}


void MainWindow::on_actLogout_triggered()
{
    ui->editPswdLogin->clear();//清除密码输入栏
    ui->stackedWidget->setCurrentWidget(ui->pagLogin);//切换至登录界面
}


void MainWindow::on_btnAppendResultUser_clicked()
{
    QString str=QInputDialog::getText(this,"输入","请输入申请的装备名:");//弹出输入装备名对话框
    if(!str.isEmpty()){//如果输入不为空
        QString id=QInputDialog::getText(this,"输入","请输入申请编号:");//弹出输入装备名对话框
        if(!id.isEmpty()){
            QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvResultUser->model());
            model->insertRow(model->rowCount());//添加表格行
            model->setData(model->index(model->rowCount()-1,0),id);//填充编号
            model->setData(model->index(model->rowCount()-1,1),m_userName);//自动填充用户名
            model->setData(model->index(model->rowCount()-1,2),str);//填充装备名
            model->setData(model->index(model->rowCount()-1,3),"未审核");//填充审核状态
        }
    }

}


void MainWindow::on_btnDeleteResultUser_clicked()
{
    QModelIndex index=ui->tvResultUser->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvResultUser->model();
        model->removeRow(index.row());
        ui->tvResultUser->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetResultUser_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvResultUser->model());
    model->select();
    for(int i=0;i<model->rowCount();++i){
        ui->tvResultUser->hideRow(i);
        if(model->data(model->index(i,1)).toString()==m_userName){
            ui->tvResultUser->showRow(i);
        }
    }
    ui->tvResultUser->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitResultUser_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvResultUser->model());
    if(model->isDirty()){
        model->submitAll();
        QMessageBox::information(this,"提示","提交成功!");
    }
}


void MainWindow::on_actApplyUser_triggered()
{
    ui->stackedWidgetUser->setCurrentWidget(ui->pagResultUser);
}


void MainWindow::on_actFixUser_triggered()
{
    ui->stackedWidgetUser->setCurrentWidget(ui->pagFixUser);
}


void MainWindow::on_actAbolisUser_triggered()
{
    ui->stackedWidgetUser->setCurrentWidget(ui->pagAbolishUser);
}


void MainWindow::on_btnAppendFixUser_clicked()
{
    QString str=QInputDialog::getText(this,"输入","请输入需要维护的装备名:");//弹出输入装备名对话框
    if(!str.isEmpty()){//如果输入不为空
        QString id=QInputDialog::getText(this,"输入","请输入维护编号:");//弹出输入装备名对话框
        if(!id.isEmpty()){
            QString shcd=QInputDialog::getText(this,"输入","请输入损坏程度:");//弹出输入装备名对话框
            if(!shcd.isEmpty()){
                QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvFixUser->model());
                model->insertRow(model->rowCount());//添加表格行
                model->setData(model->index(model->rowCount()-1,0),id);//填充编号
                model->setData(model->index(model->rowCount()-1,1),m_userName);//自动填充用户名
                model->setData(model->index(model->rowCount()-1,2),str);//填充装备名
                model->setData(model->index(model->rowCount()-1,3),shcd);//填充损坏程度
                model->setData(model->index(model->rowCount()-1,4),"未审核");//填充审核状态
            }

        }
    }
}


void MainWindow::on_btnDeleteFixUserl_clicked()
{
    QModelIndex index=ui->tvFixUser->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvFixUser->model();
        model->removeRow(index.row());
        ui->tvFixUser->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetFixUser_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvFixUser->model());
    model->select();
    for(int i=0;i<model->rowCount();++i){
        ui->tvFixUser->hideRow(i);
        if(model->data(model->index(i,1)).toString()==m_userName){
            ui->tvFixUser->showRow(i);
        }
    }
    ui->tvFixUser->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitFixUser_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvFixUser->model());
    if(model->isDirty()){
        model->submitAll();
        QMessageBox::information(this,"提示","提交成功!");
    }
}


void MainWindow::on_btnAppendAbolishUser_clicked()
{
    QString str=QInputDialog::getText(this,"输入","请输入需要报废的装备名:");//弹出输入装备名对话框
    if(!str.isEmpty()){//如果输入不为空
        QString id=QInputDialog::getText(this,"输入","请输入报废编号:");//弹出输入装备名对话框
        if(!id.isEmpty()){
            QString reson=QInputDialog::getText(this,"输入","请输入报废原因:");//弹出输入装备名对话框
            if(!reson.isEmpty()){
                QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvAbolistUser->model());
                model->insertRow(model->rowCount());//添加表格行
                model->setData(model->index(model->rowCount()-1,0),id);//填充编号
                model->setData(model->index(model->rowCount()-1,1),m_userName);//自动填充用户名
                model->setData(model->index(model->rowCount()-1,2),str);//填充装备名
                model->setData(model->index(model->rowCount()-1,3),reson);//填充报废原因
                model->setData(model->index(model->rowCount()-1,4),"未审核");//填充审核状态
            }

        }
    }
}


void MainWindow::on_btnDeleteAbolishUser_clicked()
{
    QModelIndex index=ui->tvAbolistUser->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvAbolistUser->model();
        model->removeRow(index.row());
        ui->tvAbolistUser->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetAbolishUser_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvAbolistUser->model());
    model->select();
    for(int i=0;i<model->rowCount();++i){
        ui->tvAbolistUser->hideRow(i);
        if(model->data(model->index(i,1)).toString()==m_userName){
            ui->tvAbolistUser->showRow(i);
        }
    }
    ui->tvAbolistUser->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitAbolihUser_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvAbolistUser->model());
    if(model->isDirty()){
        model->submitAll();
        QMessageBox::information(this,"提示","提交成功!");
    }
}


void MainWindow::on_actFixAdmin_triggered()
{
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagFixAdmin);
}


void MainWindow::on_actAbolish_triggered()
{
    ui->stackedWidgetAdmin->setCurrentWidget(ui->pagAbolishAdmin);
}


void MainWindow::on_btnPassFixAdmin_clicked()
{
    QModelIndex index=ui->tvFixAdmin->currentIndex();//获取当前申请管理界面的选中行
    if(index.isValid()){//如果选中行可用
        QAbstractItemModel* model=ui->tvFixAdmin->model();//获取申请界面绑定的模型
        model->setData(model->index(index.row(),4),"通过");//修改模型数据
        ui->tvFixAdmin->setCurrentIndex(QModelIndex());//置空选中行
    }
}


void MainWindow::on_btnRefuseFixAdmin_clicked()
{
    QModelIndex index=ui->tvFixAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvFixAdmin->model();
        model->setData(model->index(index.row(),4),"不通过");//修改模型数据
        ui->tvFixAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnDeleteFixAdmin_clicked()
{
    QModelIndex index=ui->tvFixAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvFixAdmin->model();
        model->removeRow(index.row());//移除数据记录
        ui->tvFixAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetFixAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvFixAdmin->model());//获取模型绑定的数据模型
    model->select();//重新从数据库加载数据到模型
    ui->tvFixAdmin->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitFixAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvFixAdmin->model());
    if(model->isDirty()){//如果模型数据被修改过
        model->submitAll();//提交所有修改到数据库
        QMessageBox::information(this,"提示","提交成功!");//弹出修改成功提示框
    }
}


void MainWindow::on_btnPassAbolishAdmin_clicked()
{
    QModelIndex index=ui->tvAbolishAdmin->currentIndex();//获取当前申请管理界面的选中行
    if(index.isValid()){//如果选中行可用
        QAbstractItemModel* model=ui->tvAbolishAdmin->model();//获取申请界面绑定的模型
        model->setData(model->index(index.row(),4),"通过");//修改模型数据
        ui->tvAbolishAdmin->setCurrentIndex(QModelIndex());//置空选中行
    }
}


void MainWindow::on_btnRefuseAbolishAdmin_clicked()
{
    QModelIndex index=ui->tvAbolishAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvAbolishAdmin->model();
        model->setData(model->index(index.row(),4),"不通过");//修改模型数据
        ui->tvAbolishAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnDeleteAbolishAdmin_clicked()
{
    QModelIndex index=ui->tvAbolishAdmin->currentIndex();
    if(index.isValid()){
        QAbstractItemModel* model=ui->tvAbolishAdmin->model();
        model->removeRow(index.row());//移除数据记录
        ui->tvAbolishAdmin->setCurrentIndex(QModelIndex());
    }
}


void MainWindow::on_btnResetAbolishAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvAbolishAdmin->model());//获取模型绑定的数据模型
    model->select();//重新从数据库加载数据到模型
    ui->tvAbolishAdmin->setCurrentIndex(QModelIndex());
}


void MainWindow::on_btnSubmitAbolishAdmin_clicked()
{
    QSqlTableModel* model=static_cast<QSqlTableModel*>(ui->tvAbolishAdmin->model());
    if(model->isDirty()){//如果模型数据被修改过
        model->submitAll();//提交所有修改到数据库
        QMessageBox::information(this,"提示","提交成功!");//弹出修改成功提示框
    }
}

